using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class PushPullCabinetItem : BaseItem
	{
		public enum Anim
		{
			Static = 0,
			LeftDoorMoveLeft = 1,
			LeftDoorMoveRight = 2,
			RightDoorMoveLeft = 3,
			RightDoorMoveRight = 4
		}

		public enum State
		{
			Left = 0,
			Right = 1
		}

		private Transform leftDoor;

		private Transform rightDoor;

		private float leftX;

		private float rightX;

		private State leftDoorState;

		private State rightDoorState = State.Right;

		private Anim anim;

		private float curX;

		private Vector2 closeCollOffset;

		private Vector2 closeCollSize;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			BoxCollider2D component = model.GetComponent<BoxCollider2D>();
			closeCollOffset = component.offset;
			closeCollSize = component.size;
			leftDoor = model.Find("Front/LeftDoor");
			leftDoor.gameObject.layer = 15;
			rightDoor = model.Find("Front/RightDoor");
			rightDoor.gameObject.layer = 15;
			rightDoor.SetLocalZ(-1E-05f);
			leftX = leftDoor.localPosition.x;
			rightX = rightDoor.localPosition.x;
			SetLeftDoorState((State)data.intParam1);
			SetRightDoorState((State)data.intParam2);
			UpdatePlatformState();
			if (leftDoorState == State.Right)
			{
				leftDoor.SetLocalX(rightX);
			}
			if (rightDoorState == State.Left)
			{
				rightDoor.SetLocalX(leftX);
			}
		}

		protected override void LateUpdate()
		{
			if (anim == Anim.Static)
			{
				return;
			}
			if (anim == Anim.LeftDoorMoveRight)
			{
				curX += Time.deltaTime * 3f;
				if (curX >= rightX)
				{
					leftDoor.SetLocalX(rightX);
					anim = Anim.Static;
				}
				else
				{
					leftDoor.SetLocalX(curX);
				}
			}
			else if (anim == Anim.LeftDoorMoveLeft)
			{
				curX -= Time.deltaTime * 3f;
				if (curX <= leftX)
				{
					leftDoor.SetLocalX(leftX);
					anim = Anim.Static;
				}
				else
				{
					leftDoor.SetLocalX(curX);
				}
			}
			else if (anim == Anim.RightDoorMoveRight)
			{
				curX += Time.deltaTime * 3f;
				if (curX >= rightX)
				{
					rightDoor.SetLocalX(rightX);
					anim = Anim.Static;
				}
				else
				{
					rightDoor.SetLocalX(curX);
				}
			}
			else if (anim == Anim.RightDoorMoveLeft)
			{
				curX -= Time.deltaTime * 3f;
				if (curX <= leftX)
				{
					rightDoor.SetLocalX(leftX);
					anim = Anim.Static;
				}
				else
				{
					rightDoor.SetLocalX(curX);
				}
			}
		}

		private void SetLeftDoorState(State val)
		{
			leftDoorState = val;
		}

		private void SetRightDoorState(State val)
		{
			rightDoorState = val;
		}

		private void UpdatePlatformState()
		{
			if (platforms == null || platforms.Count < 1)
			{
				return;
			}
			bool flag = true;
			bool flag2 = true;
			if (leftDoorState != 0 && rightDoorState != 0)
			{
				flag = false;
			}
			if (leftDoorState != State.Right && rightDoorState != State.Right)
			{
				flag2 = false;
			}
			if (flag && flag2)
			{
				coll.offset = closeCollOffset;
				coll.size = closeCollSize;
				SetPlatformColliderEnable(false);
				SetChildsColliderEnabled(false);
				return;
			}
			SetPlatformColliderEnable(true);
			SetChildsColliderEnabled(true);
			if (flag)
			{
				BoxCollider2D component = model.Find("LeftColl").GetComponent<BoxCollider2D>();
				coll.offset = component.offset;
				coll.size = component.size;
			}
			else if (flag2)
			{
				BoxCollider2D component2 = model.Find("RightColl").GetComponent<BoxCollider2D>();
				coll.offset = component2.offset;
				coll.size = component2.size;
			}
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (anim != 0)
			{
				return;
			}
			RaycastHit2D raycastHit2D = Physics2D.Raycast(touchEventData.curPos, Vector2.zero, 15f, 32768);
			if (!(raycastHit2D.collider != null))
			{
				return;
			}
			if (raycastHit2D.collider.transform == leftDoor)
			{
				if (leftDoorState == State.Left)
				{
					anim = Anim.LeftDoorMoveRight;
					SetLeftDoorState(State.Right);
					curX = leftX;
				}
				else
				{
					anim = Anim.LeftDoorMoveLeft;
					SetLeftDoorState(State.Left);
					curX = rightX;
				}
				UpdatePlatformState();
			}
			else if (raycastHit2D.collider.transform == rightDoor)
			{
				if (rightDoorState == State.Left)
				{
					anim = Anim.RightDoorMoveRight;
					SetRightDoorState(State.Right);
					curX = leftX;
				}
				else
				{
					anim = Anim.RightDoorMoveLeft;
					SetRightDoorState(State.Left);
					curX = rightX;
				}
				UpdatePlatformState();
			}
			if (base.itemData.scriptParams.Equals("1"))
			{
				Singleton<AudioManager>.Instance.PlaySound("sound_push_pull_door_2");
			}
			else
			{
				Singleton<AudioManager>.Instance.PlaySound("sound_push_pull_door_1");
			}
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["59"] = (int)leftDoorState;
			obj["60"] = (int)rightDoorState;
		}
	}
}
